package org.dataframework.security.core.db;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiuCf
 * @Email: 1316640406@qq.com
 * @Date: 2021/7/3 13:46
 */
public class RowMappers {
    public static RowMapper tableMapper() {
        return (resultSet) -> {
            List<Table> tableList = new ArrayList<>();
            while (resultSet.next()) {
                String tableName = resultSet.getString(1);
                tableList.add(new Table(tableName));
            }

            return tableList;
        };
    }

    public static RowMapper columnMapper() {
        return (resultSet) -> {
            List<Column> columnList = new ArrayList<>();
            while (resultSet.next()) {
                String tableName = resultSet.getString(1);
                String columnName = resultSet.getString(2);
                String dataType = resultSet.getString(3);
                columnList.add(new Column(new Table(tableName), columnName, dataType));
            }

            return columnList;
        };
    }

    public static RowMapper ruleMapper() {
        return (resultSet) -> {
            List<Rule> ruleList = new ArrayList<>();
            while (resultSet.next()) {
                Rule rule = new Rule();
                String tableName = resultSet.getString(1);
                String columnName = resultSet.getString(2);
                String dataType = resultSet.getString(3);
                rule.setColumn(new Column(new Table(tableName), columnName, dataType));
                String expression = resultSet.getString(4);
                Object param1 = resultSet.getObject(5);
                Object param2 = resultSet.getObject(6);
                rule.setExpression(expression);
                rule.setParam1(param1);
                rule.setParam2(param2);
                ruleList.add(rule);
            }

            return ruleList;
        };
    }
}
